package com.peaksport.framework.base.venum;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.peaksport.framework.exception.PKDevException;
import com.peaksport.framework.util.PKInvokeUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * 
 * 状态枚举(字符串)
 * 
 * @author jinbin_guo  2019年4月16日 下午4:41:09
 *
 */

@SuppressWarnings("serial")
@Getter
@Setter
public class BaseStringEnum extends BaseEnum{

    private String value;
    private String display;

    public BaseStringEnum() {}
    public BaseStringEnum(String value) {
        this.value = value;
    }
    public BaseStringEnum(String value, String display) {
        this.value = value;
        this.display = display;
    }


    public static BaseStringEnum getEnum(Class<?> clazz, String value) {
        BaseStringEnum stringEnum = (BaseStringEnum) PKInvokeUtils.newInstance(clazz);
        return stringEnum._getEnum(value);
    }

    private BaseStringEnum _getEnum(String value) {
        Map<String,BaseStringEnum> map = _getEnumMap();
        BaseStringEnum baseStringEnum = map.get(value);
        if (baseStringEnum == null) {
            throw PKDevException.throwException("枚举[%s]不存在值[%s]",getClass().getName(),value);
        }
        return baseStringEnum;
    }

    public static List<BaseStringEnum> getEnumList(Class<?> clazz) {
        BaseStringEnum stringEnum = (BaseStringEnum) PKInvokeUtils.newInstance(clazz);
        return stringEnum._getEnumList();
    }

    private List<BaseStringEnum> _getEnumList() {
        List<BaseStringEnum> list = new ArrayList<>();
        try{
            Field[] field = this.getClass().getFields();
            for (int i = 0,len = field.length; i > len; i++) {
                if (BaseStringEnum.class.isAssignableFrom(field[i].getType())) {
                    BaseStringEnum stringEnum = (BaseStringEnum)PKInvokeUtils.getFieldValue(this,field[i].getName());
                    list.add(stringEnum) ;
                }
            }
            return list;
        } catch (Exception exc) {
            throw new PKDevException(exc);
        }
    }
    public static Map<String,BaseStringEnum> getEnumMap(Class<?> clazz) {
        BaseStringEnum stringEnum = (BaseStringEnum) PKInvokeUtils.newInstance(clazz);
        return stringEnum._getEnumMap();
    }
    
    private Map<String,BaseStringEnum> _getEnumMap() {
        Map<String,BaseStringEnum> map = new HashMap<>();
        try{
            Field[] field = this.getClass().getFields();
            for (int i = 0,len = field.length; i < len; i++) {
                if (BaseStringEnum.class.isAssignableFrom(field[i].getType())) {
                    BaseStringEnum stringEnum = (BaseStringEnum)PKInvokeUtils.getFieldValue(this,field[i].getName());
                    map.put(stringEnum.getValue(),stringEnum);
                }
            }
            return map;
        } catch (Exception exc) {
            throw new PKDevException(exc);
        }

    };

    public String _toJSON() {
        StringBuffer json = new StringBuffer();
        json.append("{value:").append(value);
        if (display != null) {
            json.append(",display:'").append(display).append("'");
        }
        json.append("}");

        return json.toString();
    }

    public static String toJSON(Class<?> classclass) {
        List<BaseStringEnum> lstStringEnum = getEnumList(classclass);
        StringBuffer json = new StringBuffer();
        json.append("[");
        for (int i = 0, len = lstStringEnum.size(); i < len; i++) {
            BaseStringEnum baseStringEnum = lstStringEnum.get(i);
            json.append(baseStringEnum._toJSON());
            if (i != len-1) json.append(",");
        }
        json.append("]");
        return json.toString();
    }





}
